<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Examples from the CVX Users' guide</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/html/quickstart.html">
<link rel="stylesheet" href="../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Examples from the CVX Users' guide</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
has_quadprog = exist( <span class="string">'quadprog'</span> );
has_quadprog = has_quadprog == 2 | has_quadprog == 3;
has_linprog  = exist( <span class="string">'linprog'</span> );
has_linprog  = has_linprog == 2 | has_linprog == 3;
rnstate = randn( <span class="string">'state'</span> ); randn( <span class="string">'state'</span>, 1 );
s_quiet = cvx_quiet(true);
s_pause = cvx_pause(false);
cvx_clear; echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.1: LEAST SQUARES %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% Input data</span>
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);

<span class="comment">% Matlab version</span>
x_ls = A \ b;

<span class="comment">% cvx version</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm(A*x-b) );
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x_ls-b): %6.4f\nnorm(A*x-b):    %6.4f\ncvx_optval:     %6.4f\ncvx_status:     %s\n'</span>, norm(A*x_ls-b), norm(A*x-b), cvx_optval, cvx_status ) );
disp( <span class="string">'Verify that x_ls == x:'</span> );
disp( [ <span class="string">'   x_ls  = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x_ls ), <span class="string">']'</span> ] );
disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
disp( <span class="string">'Residual vector:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.2: BOUND-CONSTRAINED LEAST SQUARES %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% More input data</span>
bnds = randn(n,2);
l = min( bnds, [] ,2 );
u = max( bnds, [], 2 );

<span class="keyword">if</span> has_quadprog,
    <span class="comment">% Quadprog version</span>
    x_qp = quadprog( 2*A'*A, -2*A'*b, [], [], [], [], l, u );
<span class="keyword">else</span>,
    <span class="comment">% quadprog not present on this system.</span>
<span class="keyword">end</span>

<span class="comment">% cvx version</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm(A*x-b) );
    subject <span class="string">to</span>
        l &lt;= x &lt;= u;
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
<span class="keyword">if</span> has_quadprog,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x_qp-b): %6.4f\nnorm(A*x-b):    %6.4f\ncvx_optval:     %6.4f\ncvx_status:     %s\n'</span>, norm(A*x_qp-b), norm(A*x-b), cvx_optval, cvx_status ) );
    disp( <span class="string">'Verify that l &lt;= x_qp == x &lt;= u:'</span> );
    disp( [ <span class="string">'   l     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, l ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   x_qp  = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x_qp ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   u     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, u ), <span class="string">']'</span> ] );
<span class="keyword">else</span>,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x-b): %6.4f\ncvx_optval:  %6.4f\ncvx_status:  %s\n'</span>, norm(A*x-b), cvx_optval, cvx_status ) );
    disp( <span class="string">'Verify that l &lt;= x &lt;= u:'</span> );
    disp( [ <span class="string">'   l     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, l ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   u     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, u ), <span class="string">']'</span> ] );
<span class="keyword">end</span>
disp( <span class="string">'Residual vector:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.3: OTHER NORMS AND FUNCTIONS: INFINITY NORM %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="keyword">if</span> has_linprog,
    <span class="comment">% linprog version</span>
    f    = [ zeros(n,1);  1          ];
    Ane  = [ +A,          -ones(m,1) ; <span class="keyword">...</span>
             -A,          -ones(m,1) ];
    bne  = [ +b;          -b         ];
    xt   = linprog(f,Ane,bne);
    x_lp = xt(1:n,:);
<span class="keyword">else</span>,
    <span class="comment">% linprog not present on this system.</span>
<span class="keyword">end</span>

<span class="comment">% cvx version</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm(A*x-b,Inf) );
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
<span class="keyword">if</span> has_linprog,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x_lp-b,Inf): %6.4f\nnorm(A*x-b,Inf):    %6.4f\ncvx_optval:         %6.4f\ncvx_status:         %s\n'</span>, norm(A*x_lp-b,Inf), norm(A*x-b,Inf), cvx_optval, cvx_status ) );
    disp( <span class="string">'Verify that x_lp == x:'</span> );
    disp( [ <span class="string">'   x_lp  = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x_lp ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
<span class="keyword">else</span>,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x-b,Inf): %6.4f\ncvx_optval:      %6.4f\ncvx_status:      %s\n'</span>, norm(A*x-b,Inf), cvx_optval, cvx_status ) );
    disp( <span class="string">'Optimal vector:'</span> );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
<span class="keyword">end</span>
disp( sprintf( <span class="string">'Residual vector; verify that the peaks match the objective (%6.4f):'</span>, cvx_optval ) );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.3: OTHER NORMS AND FUNCTIONS: ONE NORM %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="keyword">if</span> has_linprog,
    <span class="comment">% Matlab version</span>
    f    = [ zeros(n,1); ones(m,1);  ones(m,1)  ];
    Aeq  = [ A,          -eye(m),    +eye(m)    ];
    lb   = [ -Inf*ones(n,1);  zeros(m,1); zeros(m,1) ];
    xzz  = linprog(f,[],[], Aeq,b,lb,[]);
    x_lp = xzz(1:n,:);
<span class="keyword">else</span>,
    <span class="comment">% linprog not present on this system</span>
<span class="keyword">end</span>

<span class="comment">% cvx version</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm(A*x-b,1) );
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
<span class="keyword">if</span> has_linprog,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x_lp-b,1): %6.4f\nnorm(A*x-b,1): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n'</span>, norm(A*x_lp-b,1), norm(A*x-b,1), cvx_optval, cvx_status ) );
    disp( <span class="string">'Verify that x_lp == x:'</span> );
    disp( [ <span class="string">'   x_lp  = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x_lp ), <span class="string">']'</span> ] );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
<span class="keyword">else</span>,
    disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x-b,1): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n'</span>, norm(A*x-b,1), cvx_optval, cvx_status ) );
    disp( <span class="string">'Optimal vector:'</span> );
    disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
<span class="keyword">end</span>
disp( <span class="string">'Residual vector; verify the presence of several zero residuals:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.3: OTHER NORMS AND FUNCTIONS: LARGEST-K NORM %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% cvx specification</span>
k = 5;
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm_largest(A*x-b,k) );
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
temp = sort(abs(A*x-b));
disp( sprintf( <span class="string">'\nResults:\n--------\nnorm_largest(A*x-b,k): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n'</span>, norm_largest(A*x-b,k), cvx_optval, cvx_status ) );
disp( <span class="string">'Optimal vector:'</span> );
disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
disp( sprintf( <span class="string">'Residual vector; verify a tie for %d-th place (%7.4f):'</span>, k, temp(end-k+1) ) );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.3: OTHER NORMS AND FUNCTIONS: HUBER PENALTY %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% cvx specification</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( sum(huber(A*x-b)) );
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
disp( sprintf( <span class="string">'\nResults:\n--------\nsum(huber(A*x-b)): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n'</span>, sum(huber(A*x-b)), cvx_optval, cvx_status ) );
disp( <span class="string">'Optimal vector:'</span> );
disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
disp( <span class="string">'Residual vector:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">' '</span> );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.4: OTHER CONSTRAINTS %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% More input data</span>
p = 4;
C = randn(p,n);
d = randn(p,1);

<span class="comment">% cvx specification</span>
cvx_begin
    variable <span class="string">x(n)</span>;
    minimize( norm(A*x-b) );
    subject <span class="string">to</span>
        C*x == d;
        norm(x,Inf) &lt;= 1;
cvx_end

echo <span class="string">off</span>

<span class="comment">% Compare</span>
disp( sprintf( <span class="string">'\nResults:\n--------\nnorm(A*x-b): %6.4f\ncvx_optval: %6.4f\ncvx_status: %s\n'</span>, norm(A*x-b), cvx_optval, cvx_status ) );
disp( <span class="string">'Optimal vector:'</span> );
disp( [ <span class="string">'   x     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, x ), <span class="string">']'</span> ] );
disp( <span class="string">'Residual vector:'</span> );
disp( [ <span class="string">'   A*x-b = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, A*x-b ), <span class="string">']'</span> ] );
disp( <span class="string">'Equality constraints:'</span> );
disp( [ <span class="string">'   C*x   = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, C*x ), <span class="string">']'</span> ] );
disp( [ <span class="string">'   d     = [ '</span>, sprintf( <span class="string">'%7.4f '</span>, d   ), <span class="string">']'</span> ] );

<span class="keyword">try</span>, input( <span class="string">'Press Enter/Return for the next example...'</span> ); clc; <span class="keyword">catch</span>, <span class="keyword">end</span>
echo <span class="string">on</span>

<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>
<span class="comment">% SECTION 2.5: AN OPTIMAL TRADEOFF CURVE %</span>
<span class="comment">%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%</span>

<span class="comment">% The basic problem:</span>
<span class="comment">% cvx_begin</span>
<span class="comment">%     variable x(n);</span>
<span class="comment">%     minimize( norm(A*x-b)+gamma(k)*norm(x,1) );</span>
<span class="comment">% cvx_end</span>

echo <span class="string">off</span>
disp( <span class="string">' '</span> );
disp( <span class="string">'Generating tradeoff curve...'</span> );
cvx_pause(false);
gamma = logspace( -2, 2, 20 );
l2norm = zeros(size(gamma));
l1norm = zeros(size(gamma));
fprintf( 1, <span class="string">'   gamma       norm(x,1)    norm(A*x-b)\n'</span> );
fprintf( 1, <span class="string">'---------------------------------------\n'</span> );
<span class="keyword">for</span> k = 1:length(gamma),
    fprintf( 1, <span class="string">'%8.4e'</span>, gamma(k) );
    cvx_begin
        variable <span class="string">x(n)</span>;
        minimize( norm(A*x-b)+gamma(k)*norm(x,1) );
    cvx_end
    l1norm(k) = norm(x,1);
    l2norm(k) = norm(A*x-b);
    fprintf( 1, <span class="string">'   %8.4e   %8.4e\n'</span>, l1norm(k), l2norm(k) );
<span class="keyword">end</span>
plot( l1norm, l2norm );
xlabel( <span class="string">'norm(x,1)'</span> );
ylabel( <span class="string">'norm(A*x-b)'</span> );
grid
disp( <span class="string">'Done. (Check out the graph!)'</span> );
randn( <span class="string">'state'</span>, rnstate );
cvx_quiet(s_quiet);
cvx_pause(s_pause);
</pre>
<a id="output"></a>
<pre class="codeoutput">
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.1: LEAST SQUARES %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Input data
m = 16; n = 8;
A = randn(m,n);
b = randn(m,1);

% Matlab version
x_ls = A \ b;

% cvx version
cvx_begin
    variable x(n);
    minimize( norm(A*x-b) );
cvx_end

echo off

Results:
--------
norm(A*x_ls-b): 2.0354
norm(A*x-b):    2.0354
cvx_optval:     2.0354
cvx_status:     Solved

Verify that x_ls == x:
   x_ls  = [ -0.2628  0.8828 -0.0734 -1.0844  0.3249 -0.3330  0.0603  0.3802 ]
   x     = [ -0.2628  0.8828 -0.0734 -1.0844  0.3249 -0.3330  0.0603  0.3802 ]
Residual vector:
   A*x-b = [ -0.3262 -0.0070 -0.9543  0.2447 -0.6418 -0.3426 -0.1870  0.2960  0.6024 -0.0440  0.6238 -0.7399  0.0849  0.9323  0.4799 -0.0762 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.2: BOUND-CONSTRAINED LEAST SQUARES %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% More input data
bnds = randn(n,2);
l = min( bnds, [] ,2 );
u = max( bnds, [], 2 );

if has_quadprog,
else,
    % quadprog not present on this system.
end

% cvx version
cvx_begin
    variable x(n);
    minimize( norm(A*x-b) );
    subject to
        l &lt;= x &lt;= u;
cvx_end

echo off

Results:
--------
norm(A*x-b): 4.6107
cvx_optval:  4.6107
cvx_status:  Solved

Verify that l &lt;= x &lt;= u:
   l     = [  0.3850 -0.2789 -0.9829 -0.9287 -3.0518 -0.0485  0.3182 -0.9417 ]
   x     = [  0.3850  0.6888 -0.1196 -0.8562  0.2923 -0.0485  0.4074 -0.6355 ]
   u     = [  0.7395  0.9403 -0.0290  1.6208  0.7450  2.4881  0.6919 -0.6355 ]
Residual vector:
   A*x-b = [  1.8604  0.2157 -0.2113  2.2664 -1.5320 -0.9598 -1.6815  0.2551  1.1079  0.5313  1.2390 -0.7555  0.2176 -0.3833  1.1950 -1.0823 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.3: OTHER NORMS AND FUNCTIONS: INFINITY NORM %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if has_linprog,
else,
    % linprog not present on this system.
end

% cvx version
cvx_begin
    variable x(n);
    minimize( norm(A*x-b,Inf) );
cvx_end

echo off

Results:
--------
norm(A*x-b,Inf): 0.7079
cvx_optval:      0.7079
cvx_status:      Solved

Optimal vector:
   x     = [ -0.0944  0.8498 -0.1119 -1.1311  0.3804 -0.3017  0.2201  0.2488 ]
Residual vector; verify that the peaks match the objective (0.7079):
   A*x-b = [ -0.0431 -0.0539 -0.7079  0.7079 -0.7079 -0.7079 -0.1800  0.5049  0.7079 -0.0040  0.7079 -0.7079 -0.1010  0.7079  0.7079 -0.2187 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.3: OTHER NORMS AND FUNCTIONS: ONE NORM %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if has_linprog,
else,
    % linprog not present on this system
end

% cvx version
cvx_begin
    variable x(n);
    minimize( norm(A*x-b,1) );
cvx_end

echo off

Results:
--------
norm(A*x-b,1): 5.3359
cvx_optval: 5.3359
cvx_status: Solved

Optimal vector:
   x     = [ -0.3550  0.8934 -0.0375 -1.1827  0.1694 -0.3870 -0.2148  0.6712 ]
Residual vector; verify the presence of several zero residuals:
   A*x-b = [ -0.7666  0.0129 -1.4977 -0.0000 -0.5074  0.0000  0.0000  0.0357  0.0000 -0.0000  0.0299 -1.0842  0.0000  1.4013  0.0000  0.0000 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.3: OTHER NORMS AND FUNCTIONS: LARGEST-K NORM %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cvx specification
k = 5;
cvx_begin
    variable x(n);
    minimize( norm_largest(A*x-b,k) );
cvx_end

echo off

Results:
--------
norm_largest(A*x-b,k): 3.5394
cvx_optval: 3.5394
cvx_status: Solved

Optimal vector:
   x     = [ -0.0944  0.8498 -0.1119 -1.1311  0.3804 -0.3017  0.2201  0.2488 ]
Residual vector; verify a tie for 5-th place ( 0.7079):
   A*x-b = [ -0.0431 -0.0539 -0.7079  0.7079 -0.7079 -0.7079 -0.1800  0.5049  0.7079 -0.0040  0.7079 -0.7079 -0.1010  0.7079  0.7079 -0.2187 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.3: OTHER NORMS AND FUNCTIONS: HUBER PENALTY %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% cvx specification
cvx_begin
    variable x(n);
    minimize( sum(huber(A*x-b)) );
cvx_end

echo off

Results:
--------
sum(huber(A*x-b)): 4.1428
cvx_optval: 4.1428
cvx_status: Solved

Optimal vector:
   x     = [ -0.2628  0.8828 -0.0734 -1.0844  0.3249 -0.3330  0.0603  0.3802 ]
Residual vector:
   A*x-b = [ -0.3262 -0.0070 -0.9543  0.2447 -0.6418 -0.3426 -0.1870  0.2960  0.6024 -0.0440  0.6238 -0.7399  0.0849  0.9323  0.4799 -0.0762 ]
 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.4: OTHER CONSTRAINTS %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% More input data
p = 4;
C = randn(p,n);
d = randn(p,1);

% cvx specification
cvx_begin
    variable x(n);
    minimize( norm(A*x-b) );
    subject to
        C*x == d;
        norm(x,Inf) &lt;= 1;
cvx_end

echo off

Results:
--------
norm(A*x-b): 7.9874
cvx_optval: 7.9874
cvx_status: Solved

Optimal vector:
   x     = [  0.5085  0.4243  0.8321 -0.5836  0.2810  0.9406  0.0218 -0.6629 ]
Residual vector:
   A*x-b = [  1.9546 -0.8458 -1.4935  3.9954 -2.5173  0.3568 -3.1377 -2.0091  1.2739  2.7589  2.5399  0.0131  0.8935 -1.4236 -0.4004 -1.4326 ]
Equality constraints:
   C*x   = [  1.2841 -0.7301 -1.4727 -1.5944 ]
   d     = [  1.2841 -0.7301 -1.4727 -1.5944 ]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SECTION 2.5: AN OPTIMAL TRADEOFF CURVE %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The basic problem:
% cvx_begin
%     variable x(n);
%     minimize( norm(A*x-b)+gamma(k)*norm(x,1) );
% cvx_end
echo off
 
Generating tradeoff curve...
   gamma       norm(x,1)    norm(A*x-b)
---------------------------------------
1.0000e-02   3.3796e+00   2.0355e+00
1.6238e-02   3.3658e+00   2.0357e+00
2.6367e-02   3.3433e+00   2.0362e+00
4.2813e-02   3.3067e+00   2.0374e+00
6.9519e-02   3.2473e+00   2.0408e+00
1.1288e-01   3.1498e+00   2.0497e+00
1.8330e-01   2.9878e+00   2.0737e+00
2.9764e-01   2.7617e+00   2.1280e+00
4.8329e-01   2.3445e+00   2.2924e+00
7.8476e-01   1.4081e+00   2.8895e+00
1.2743e+00   3.7696e-01   3.9509e+00
2.0691e+00   1.0084e-08   4.4813e+00
3.3598e+00   2.8389e-11   4.4813e+00
5.4556e+00   5.7719e-11   4.4813e+00
8.8587e+00   1.4442e-11   4.4813e+00
1.4384e+01   4.6694e-10   4.4813e+00
2.3357e+01   1.7758e-11   4.4813e+00
3.7927e+01   9.1445e-13   4.4813e+00
6.1585e+01   3.3438e-12   4.4813e+00
1.0000e+02   2.1798e-14   4.4813e+00
Done. (Check out the graph!)
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="quickstart__01.png" alt=""> 
</div>
</div>
</body>
</html>